package com.cocolover2.andbase;

import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;

import com.cocolover2.andbase.annotations.PermissionCode;
import com.cocolover2.andbase.contants.PermissionCodes;

/**
 * 基本Activity
 */
public abstract class AndBaseActivity extends AppCompatActivity {



    /**
     * 检测是否拥有该功能的相关权限
     *
     * @param permissionCode 权限检测返回码
     * @return true:已授权,false:未授权
     * @since 1.1.1
     */
    public boolean checkHasPermission(@PermissionCode int permissionCode) {
        final String permission = PermissionCodes.Permissions[permissionCode];
        if (ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED) {
            return true;
        } else {
            //申请授权
            ActivityCompat.requestPermissions(this, new String[]{permission}, permissionCode);
            return false;
        }
    }

    @Override
    public void onRequestPermissionsResult(@PermissionCode int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        final boolean isGrant = grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED;
        handelPermissionGrant(requestCode, isGrant);
    }

    /**
     * 用于处理用户点击授权对话框后续业务逻辑
     *
     * @param permissionCode {@linkplain PermissionCode}
     * @param isGrant        true 授权
     * @since 1.1.2
     */
    public void handelPermissionGrant(@PermissionCode int permissionCode, boolean isGrant) {
    }

}
